import {
  addUserInfo,
  getPersonInfoApi,
  upDataPersonApi,
  updateFamilyArchiveStatus
} from "../../services/filing/filing"
import {
  GETSTORAGE,
  SETSTORAGE
} from "../../services/storage/storage"
import Message from '../../miniprogram_npm/tdesign-miniprogram/message/index'
Page({
  data: {
    activeValues: [0],
    nameValue: '',
    sexValue: '点击选择性别',
    phoneNumber: '',
    addressValue: '',
    IDValue: '',
    ageValue: '',
    phoneError: false,
    nameError: false,
    sexError: false,
    showWarnConfirm: false,
    //  picker
    citys: [{
        label: '男',
        value: '男'
      },
      {
        label: '女',
        value: '女'
      },
    ],
  },
  onPickerChange(e) {
    const {
      key
    } = e.currentTarget.dataset;
    const {
      value
    } = e.detail;

    console.log('picker change:', e.detail);
    this.setData({
      [`${key}Visible`]: false,
      sexValue: value[0],
      [`${key}Text`]: value.join(' '),
    });
  },
  onPickerCancel(e) {
    const {
      key
    } = e.currentTarget.dataset;
    console.log(e, '取消');
    console.log('picker1 cancel:');
    this.setData({
      [`${key}Visible`]: false,
    });
  },

  onWithoutTitlePicker() {
    this.setData({
      city2Visible: true,
      city2Title: ''
    });
  },
  // --------------------------
  onAgeInput(e) {
    const {
      value
    } = e.detail
    this.setData({
      ageValue: value
    })
  },
  onIDInput(e) {
    const {
      value
    } = e.detail
    this.setData({
      IDValue: value
    })
  },
  onAddressInput(e) {
    const {
      value
    } = e.detail
    this.setData({
      addressValue: value
    })
  },
  // 性别输入
  onSexInput(e) {
    const {
      value
    } = e.detail
    if (value == '男' || value == '女' || value == '') {
      this.setData({
        sexError: false,
        sexValue: value
      })
    } else {
      this.setData({
        sexError: true,
        sexValue: value
      })
    }
  },
  // 姓名输入框
  onNameInput(e) {
    const {
      value
    } = e.detail
    if (value == '') {
      this.setData({
        nameError: true,
        nameValue: value
      })
    } else {
      this.setData({
        nameError: false,
        nameValue: value
      })
    }
  },
  handleChange(e) {
    this.setData({
      activeValues: e.detail.value,
    });
  },
  //  手机号输入
  onPhoneInput(e) {
    const {
      phoneError
    } = this.data;
    const isPhoneNumber = /^[1][3,4,5,7,8,9][0-9]{9}$/.test(e.detail.value);
    if (!e.detail.value) {
      this.setData({
        phoneError: false,
        phoneNumber: e.detail.value
      })
      return
    }
    if (phoneError === isPhoneNumber) {
      this.setData({
        phoneError: !isPhoneNumber,
        phoneNumber: e.detail.value
      });
    }
  },
  // 下一步
  skip() {
    wx.navigateTo({
      url: `/pages/filing/family/family?sex=${this.data.sexValue}`
    })
  },
  // 获取初始信息
  getinfo() {
    const userId = GETSTORAGE('userId')
    getPersonInfoApi({
      userId
    }).then(res => {
      console.log(res);
      const {
        address,
        age,
        sex,
        telephone,
        nickName,
        personId,
        idcard
      } = res.data.data
      this.setData({
        nameValue: nickName,
        phoneNumber: telephone,
        addressValue: address,
        IDValue: idcard,
        ageValue: age,
        sexValue: sex
      })
    })
  },
  onLoad(options) {
    this.getinfo()
  },
  nextStep() {
    const personId = GETSTORAGE('personId')
    const obj = {
      personId: personId,
      nickName: this.data.nameValue,
      age: this.data.ageValue,
      telephone: this.data.phoneNumber,
      idcard: this.data.IDValue,
      address: this.data.addressValue,
      sex: this.data.sexValue
    }
    upDataPersonApi(obj).then(res => {
      console.log(res, '修改成功');
      if (!this.data.phoneError && !this.data.sexError && !this.data.nameError && this.data.phoneNumber != '' && this.data.nameValue != '' && this.data.sexValue != '') {
        Message.info({
          context: this,
          offset: [20, 32],
          duration: 5000,
          icon: false,
          content: '修改成功',
        });
        wx.navigateBack(-1)
      } else {
        if (this.data.phoneError || this.data.sexError && this.data.nameError) {
          Message.error({
            context: this,
            offset: [20, 32],
            duration: 2000,
            content: '输入内容不正确',
          });
          return
        } else {
          Message.error({
            context: this,
            offset: [20, 32],
            duration: 2000,
            content: '输入框不能为空',
          });
          return
        }
      }
    })
  },


  // nextStep() {
  //   //  获取userId
  //   const userId = GETSTORAGE('userId')
  //   if (!this.data.phoneError && !this.data.sexError && !this.data.nameError && this.data.phoneNumber != '' && this.data.nameValue != '' && this.data.sexValue != '') {
  //     const obj = {
  //       userId: userId,
  //       nickName: this.data.nameValue,
  //       sex: this.data.sexValue[0],
  //       mobilePhoneNumber: this.data.phoneNumber,
  //       age: this.data.ageValue,
  //       address: this.data.addressValue
  //     }
  //     // 自主建档接口
  //     addUserInfo(obj).then(res => {
  //       console.log("自主建档信息", obj);
  //       console.log("自主建档接口res=>", res);
  //       if (res.data.msg == "用户已存在") {
  //         this.setData({
  //           showWarnConfirm: true
  //         })
  //         return
  //       }
  //       //  获取用户个人信息
  //       getPersonInfoApi({
  //         userId: userId
  //       }).then(res => {
  //         console.log(res, "获取用户个人信息");
  //         SETSTORAGE('personId', res.data.data.personId)
  //       })
  //       const {
  //         familyId,
  //         familyMemberId
  //       } = res.data.data
  //       //  存储familyId
  //       SETSTORAGE('familyId', familyId)
  //       // 存储familyMemberId
  //       SETSTORAGE('familyMemberId', familyMemberId)
  //       console.log(familyId, 'familyIdfamilyIdfamilyIdfamilyIdfamilyId')
  //       //  更新建档状态
  //       updateFamilyArchiveStatus({
  //         familyId: familyId,
  //         status: 'member'
  //       }).then(res => {
  //         console.log("更新建档状态", res);
  //       })
  //       wx.navigateTo({
  //         url: `/pages/filing/family/family?sex=${this.data.sexValue}`
  //       })
  //     })
  //   } else {
  //     if (this.data.phoneError || this.data.sexError && this.data.nameError) {
  //       Message.error({
  //         context: this,
  //         offset: [20, 32],
  //         duration: 2000,
  //         content: '输入内容不正确',
  //       });
  //       return
  //     } else {
  //       Message.error({
  //         context: this,
  //         offset: [20, 32],
  //         duration: 2000,
  //         content: '输入框不能为空',
  //       });
  //       return
  //     }
  // }
  // }
});